/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.model;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class representing a combination of value and units
 * @author asanchep
 *
 */
public class ValueTimeUnit extends ValueUnit{
	private double value;
	private TimeStrings unit;
	
	/**
	 * Creates a ValueUnit object
	 * @param value the value
	 * @param unit the units in which the value is measured
	 */
	public ValueTimeUnit(double value, String unit){
		super(value,unit);
	}
	
	public ValueTimeUnit(double value, TimeStrings unit){
		super(value,unit.toString());
	}

	public void setTimeUnit(TimeStrings unit) {
		this.unit=unit;
	}
	
	public TimeStrings getTimeUnit(){
		return this.unit;
	}

	public double getValue() {
		return value;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public String getUnit() {
		return unit.toString();
	}

	public void setUnit(String unit) {
		this.unit = TimeStrings.valueOf(unit);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("vu=");
		return sb.append(value).append(unit).toString();
	}
	
	public static ValueTimeUnit stringToVTU(String vtu){
		Pattern PATTERN = Pattern.compile("[0-9\\.E]+");
		Matcher m = PATTERN.matcher(vtu);
		if(m.find()){
			String num = vtu.substring(m.start(0),m.end(0));
			String units = vtu.substring(m.end(0)); 
			double res = 0;
			try{
				res = Double.parseDouble(num);
			}catch(NumberFormatException nfe){
				return null;
			}
			return new ValueTimeUnit(res, units);
		}
		return null;
	}
	
}
